<?
/* / / / / ENGINE  CONTROL / / / / / / 
|	     CONTROL SUPERCLASS           |
|        (aka. CMS object)            |
|                                     |
| version:     0.2.0                  |
| last edit:   20100203               |
| package:     beta 2                 |
/ / / / / / / / / / / / / / / / / / /*/


class CONTROL extends CORE {
	function __construct( $vars ) {
		
		$this->_addvars( $vars );

#		Create js Element renderer
		$this->render = new CONTROL_RENDER( $this );
	}

#	Return Window content
	function asset() {
		
		if( $this->action )
			return $this->{ "filter_" . $this->action }();
	}

#	Small data transfers
	function ajax() {
		
		if( $this->action )
			return $this->{ "filter_" . $this->action }();
	}
	

###	FLOW SWEETENERS

#	Comment. (to be extended in future)
	function comment( $string ) {
	
		return "\ndashboard.comment( \"$string\" );";
	}


###	OBJECT REQUESTS

#	Create object.
	function create_object() {
		global $Object;
		
		//if( $this->type ) $this->vars->type = $this->type;
		
		return $Object = ( $this->action == "new" )?
			new OBJECT( $this->vars, 0, "..." ) : new OBJECT( $this->vars );
	
	}

#	New Object.
	function filter_new() {
		global $Object;
		
		if( $this->parent ) $new = "$('$this->parent').seed([" . $Object->render_core_json() . "], 0);";

		return $new . $this->filter_edit(); 
	}
	
#	Edit Object.
	function filter_edit() {
		global $Engine, $Object;
		
		$type = $this->vars->type;
		
		return "
		ref.seed([
			
			new Element( 'h1' ).update( \"Beheer $type '". (( $Object->get( "nameid" ) )? $Engine->text->get( $Object->get( "nameid" ) ) : $Object->id ) . "'\" ),
			new Textfield({ descr:'name $type', type:'$type', parentid:{$Object->id}, textid:". $Object->get( "nameid" ) .", values:". json_encode( $Engine->text->filter( $Object->get( "nameid" ) )) ." }),
			new Htmlfield({ descr:'text $type', type:'$type', parentid:{$Object->id}, textid:". $Object->get( "textid" ) .", values:". json_encode( $Engine->text->filter($Object->get( "textid" ), 'long' )) ." }),
			new Options({ descr:'Opties', floats: [
				new Shift({ type:'$type', descr:'Is public', value:" . $Object->get( 'active' ) . ", field:'active', objid:{$Object->id} }),
				new Rank({ type:'$type', descr:'Rank', value:" . $Object->get( 'rank' ) . ", field:'rank', objid:{$Object->id} })
			]})
		]);";

	}
	
#	Object list.
	function filter_list() {
		global $Request;
		
		if( $this->csvlist )
			$upload = "
			, new Icon( 'upload', function( succes ){
				
				window.location = init.url + '/list.csv?type=user&meta=" . json_encode( $this->csvlist ) . "';
				succes();
				
				
				//options = { onSuccess: succes, parameters: { set: Object.toJSON({ transfer_type: 'js', type:'{$this->type}', action:'csv', vars: { columns: " . json_encode( $this->csvlist ) . " }}) }};
				//new Ajax.Request( 'control.call.php', options );
			})
			";
		
		$list = ( $this->parse_list_query )?
				  $Request->parse_list( $this->parse_list_query ) : $Request->parse_list();
		
		return "
		ref.seed([
			new Element( 'h1' ).update( '" . ucfirst( $this->vars->descr ) . "' ),
			lst = new List({ type: ref.vars.type, descr:'" . (( $this->vars->boxdescr )? $this->vars->boxdescr : $this->vars->descr ) . "', blank:'add {$this->vars->type}'})
			$upload
		]);
		
		lst.seed( $list );";
		
	}


#	Object search
	function filter_search() {
		global $Engine, $Request;
		
		$type = $this->vars->type;
				
		return "
		ref.seed([
			new Element( 'h1' ).update( '" . ucfirst( $this->vars->descr ) . "' ),
			new Searchfield({ descr:'seach $type', type:'$type', value:'search term', trg:'" . $Request->trg . "' })
		]);";
		
	}
	
#	Object searcher
	function filter_quest() {
		global $DB, $Request;
				
		if ( $items = $DB->filter_objects( $Request->type, 'active AND ( SUBSTR( <%lang:nameid%>, 1, 150) LIKE "%' . $Request->value . '%" OR SUBSTR( <%lang:textid%>, 1, 450) LIKE "%' . $Request->value . '%" ) ORDER BY <%lang:nameid%> ASC' )) 
		{	
			foreach( $items as $item ) {
				//echo $item->get( 'name' );
				$nodes[] = array( 
					'id'	=> $item->id,
					'type'	=> $Request->type,
					'rank'	=> (( $item->get( 'rank' ) && $item->get( 'rank' ) >= 0 && $item->get( 'rank' ) < 11 ) ? $item->get( 'rank' ) : '0' ),
					'list'	=> array( $item->id, $item->get( 'name' ), $item->get( 'active' ))
				);
			}
		} else {
			$nodes[] = array(
				'id'	=> 0,
				'type'	=> $Request->type,
				'rank'	=> 0,
				'list'	=> array( 0, 'no results', 0 )
			);
		}
		
		return "
		$('column_left').insert( ref = new Win( '" . $Request->type . "' ));
		
		ref.seed([
			new Element( 'h1' ).update( 'search " . $Request->type . " for `" . $Request->value . "`' ),
			lst = new List({ type: '" . $Request->type . "', descr:'search " . $Request->type . " for `" . $Request->value . "`'})
		]);
		
		lst.seed( ". json_encode( $nodes ) ." );";
	}

#	Kill.
	function filter_kill() {
		global $Object;
		
		( $Object->get( "active" ) )?
			$Object->dump() : $Object->remove();


		return $this->comment( ucfirst( $this->vars->type ) ." ". (( $Object->get( "active" ) )? "disabled.": "removed." ));
	}

#	Change hierarchy.
	function filter_sortable() {
		
		return $this->comment( "Functionality not set (yet)..." );
	}

#	Shift (alter column value).
	function filter_ajax() {
		global $DB, $Request;
		
		$comment = ( $Request->ajax() )? "option modified":"warning: failed";

		return $this->comment( $comment );
	}
	
#	Referentials request (Select§).
	function filter_referential() {
		global $Request;
		
		return $Request->referential();
	}
	
#	Filter Flow request.
	function filter_flow() {
		global $Request;
		
		return "$('control_list')._push( ref, " . $Request->flow() . " );";
	}
	
#	List update.
	function filter_update_list() {
		global $Request;
		
		return "ref.seed( ". $Request->parse_list() ." );";
		
	}

}


/** 	REQUEST FUNCS
* Set ajax request set and functions
* 											 */
class CONTROL_RENDER extends CORE  {
	function __construct( $set ) {
		
		$this->_addvars( $set );
	}

#	Render js Dropbox with media content
	//new Dropbox({ descr:'Beelden', type:'media.img', parenttype:'$type', action:'win', blank:'add image', id:{$Object->id}, uptype:'media', floats:". $this->filter_media( 'media.img' ) ." }),
	//new Dropbox({ descr:'Streams', type:'media.stream', parenttype:'$type', action:'win', blank:'add stream', parentid:{$Object->id}, uptype:'stream', floats:". $this->filter_media( 'media.stream' ) ." }),
	
	//meta-wise:
	//$this->render->Select( "country", $Object, "name", "land", true );
	
#	Render js Select Element
/*	public function Select( $type, $object, $field, $descr = false, $is_meta = false ) {
		global $DB;
		
		if( $list = $DB->filter_objects( $type, "active order by rank desc" ))
			foreach ( $list as $obj )
				$select[ 'name' ] = $obj->get( $field );
		
		if( !$descr ) $descr = $field;
		if( $is_meta ) $field = "meta.$field"; 
		
		return "new Select({ type: '{$this->vars->type}', descr:'$descr', objid: {$object->id}, field: '$field' value: '{$Object->get( 'ctry' )}', opts: " . json_encode( $select ) . " })";
	}
	
	//ref-wise:
	//$this->render->Select( "country", $Object, "name", "land", false );
*/	
#	Render js Select Element, reference based
/*	public function Select_ref( $type, $object, $field, $descr = false, $is_master = false ) {
		global $DB, $Object;
		
		
		
		$parenttype	= $is_master? $object->type: $type;
		$objtype	= $is_master? $type: $object->type;
		
		if( $id = $is_master? $DB->reference( $parenttype, $object->id, $objtype ) : $DB->reference_by_child( $parenttype, $objtype, (int) $object->id ))
			$id = array_shift( $id );
		
		if( $list = $DB->filter_objects( $type, "active order by rank desc" ))
			foreach ( $list as $obj ) {
				$select[ $obj->get( $field )] = $obj->id;
				if( $id == $obj->id ) $value = $obj->get( $field );
			}
		
		if( !$descr ) $descr = $field;
		
		$parentid	= (int) $is_master? $object->id : $id;
		$objid		= (int) $is_master? $id : $object->id;
		
		
		return "new Select({ type: '{$this->vars->type}', descr:'$descr', objid: {$object->id}, ref:{ parenttype:'$parenttype', objtype:'$objtype', parentid:'$parentid', objid:$objid, master:" . (int) $is_master . "}, value: '$value', opts: " . json_encode( $select ) . " })";
	}
	*/
	
#	Render js Select Element
#	For options:		Select( str $descr, array $options, str $field (dotted for meta) [, 0, str $function ] );
#	For referential:	Select( str $descr, str $second_type, int $master/$child, 1 [, str $function ] );

	public function Select( $descr, $options_or_type = array(), $field_or_master = 0, $is_reference = 0, $relation = 0, $func = false, $multiple = false  ) {
		global $DB, $Object;
		
		$type	= $Object->type;
		if( $is_reference )
			if( $multiple == 1 )
				$ref = $this->render_references( $options_or_type, $field_or_master, (( stristr( $options_or_type, 'tag' )) ? 'tag' : 'object' ));
			else 
				$ref = $this->render_reference( $options_or_type, $field_or_master );
			
		else if( is_string( $options_or_type ))
			if(( $key = $Object->get( $field_or_master )) && ( $Child = new OBJECT( $options_or_type, (int) $key )))
				 $opts = "field: '$field_or_master', objtype: '$options_or_type', key: '$key', value: '{$Child->get( 'name' )}'";
			else $opts = "field: '$field_or_master', objtype: '$options_or_type', key: '$key'";
		else $opts = "field: '$field_or_master', opts: ". json_encode( $options_or_type ) .", key: '{$Object->get( $field_or_master )}'";
		
		if( $ref && is_array( $ref )) {
			$t = 4;
			foreach( $ref as $r ) {
				$return[] = "sel" . $t . " = new Select({ type:'{$Object->type}', descr:'$descr', objid: {$Object->id}, {$r}{$opts}" . (( $func )? ", func: '$func'" : "" ) . (( $multiple ) ? ", multiple: 1" : "" ) . ", relation:$relation })";
				$t++;
			}
			return implode( ', ', $return );
		} else {
			return "new Select({ type:'{$Object->type}', descr:'$descr', objid: {$Object->id}, {$ref}{$opts}" . (( $func )? ", func: '$func'" : "" ) . (( $multiple ) ? ", multiple: 1" : "" ) . ", relation:$relation })";
		}
	}

	
#	Retreive Select reference input
	public function render_references( $type, $is_master = 0, $tablestring = 'object' ) {
		global $DB, $Object;
		
		if( $refs = $Object->ref_objects( $type, $is_master, 240, $tablestring )) {
			foreach( $refs as $ref ) 
				$keys[] = $ref->id;
		}
		else {
			$keys[] = 0;
		}
		foreach( array_unique( $keys ) as $key ) {
			$parenttype	= $is_master? $Object->type: $type;
			$objtype	= $is_master? $type: $Object->type;
			$parentid	= $is_master? $Object->id: $key;
			$objid		= $is_master? $key: $Object->id;

			$options = $DB->filter_key( $type, "active order by rank desc, value limit 36", "ID as `key`, <%lang:nameid%> as value" );
			$return[] =  "key:" . $key . ", ref:{ parenttype:'$parenttype', objtype:'$objtype', parentid:$parentid, objid: $objid, master:$is_master, single:1 }, opts: ". json_encode( $options );
		}
		return $return;
	}

#	Retreive Select reference input
	public function render_reference( $type, $is_master = 0 ) {
		global $DB, $Object;
		
		if( !$key = $Object->ref_object( $type, $is_master, (( $type != 'tag' )? 'object': 'tag' ))->id ) $key = 0;

		$parenttype	= $is_master? $Object->type: $type;
		$objtype	= $is_master? $type: $Object->type;
		$parentid	= $is_master? $Object->id: $key;
		$objid		= $is_master? $key: $Object->id;

		/*if( $id = $is_master? $DB->reference( $parenttype, $this->parentid = $Object->id, $objtype ) : $DB->reference_by_child( $parenttype, $objtype, $this->objid = $Object->id ))
			$this->{ ( $is_master )? "objid": "parentid" } = array_shift( $id );*/
			
		$options = $DB->filter_key( $type, "active order by rank desc, value limit 36", "ID as `key`, <%lang:nameid%> as value" );
		
		return "key:$key, ref:{ parenttype:'$parenttype', objtype:'$objtype', parentid:$parentid, objid: $objid, master:$is_master, single:1 }, opts: ". json_encode( $options );
	}

#	Render Meta data, based on json object and custom control.request class
#	For options:		Select( str $descr, array $options, str $field (dotted for meta) [, 0, str $function ] );
	public function Meta( $descr ) {
		global $Object;
		
		if( $meta = $Object->values[ 'meta' ])
			foreach( $meta as $key => $val ) if( is_string( $val ) || is_int( $val )) $opts[] = "new Ajxfield({ type:'{$Object->type}', descr:'$key', objid:{$Object->id}, field:'meta.$key', value:\"$val\" })";
		
		return ( $opts )?
			 "new Options({ descr:'{$descr}', floats: [" . implode( ",", $opts ) . "]})" :
			 "'$descr'";
			
		
	}
}

?>